<template>
  <div id="app">
    <el-container class="app-container">
      <!-- 顶部导航 -->
      <el-header class="app-header">
        <div class="header-content">
          <div class="logo-section">
            <img src="/devenv-logo.png" alt="DevEnv Logo" class="app-logo" />
            <h1 class="app-title">DevEnv 智能开发环境部署工具</h1>
          </div>

          <div class="nav-section">
            <el-menu :default-active="$route.path" mode="horizontal" router class="nav-menu">
              <el-menu-item index="/">
                <el-icon>
                  <House />
                </el-icon>
                <span>首页</span>
              </el-menu-item>
              <el-menu-item index="/software">
                <el-icon>
                  <Box />
                </el-icon>
                <span>软件选择</span>
              </el-menu-item>
              <el-menu-item index="/template-market">
                <el-icon>
                  <Shop />
                </el-icon>
                <span>模板广场</span>
              </el-menu-item>
              <el-menu-item index="/script">
                <el-icon>
                  <DocumentCopy />
                </el-icon>
                <span>脚本生成</span>
              </el-menu-item>
            </el-menu>

            <!-- 用户区域 -->
            <div class="user-section">
              <div v-if="userStore.isLoggedIn" class="user-info">
                <!-- 通知铃铛 -->
                <NotificationBell />

                <el-dropdown @command="handleUserCommand">
                  <span class="user-dropdown">
                    <el-avatar :size="32" :src="userStore.user?.avatarUrl">
                      <el-icon>
                        <User />
                      </el-icon>
                    </el-avatar>
                    <span class="username">{{ userStore.user?.nickname || userStore.user?.username }}</span>
                    <el-icon class="el-icon--right">
                      <ArrowDown />
                    </el-icon>
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="profile">
                        <el-icon>
                          <User />
                        </el-icon>
                        个人中心
                      </el-dropdown-item>
                      <el-dropdown-item command="notifications">
                        <el-icon>
                          <Star />
                        </el-icon>
                        我的通知
                      </el-dropdown-item>
                      <el-dropdown-item command="templates">
                        <el-icon>
                          <Document />
                        </el-icon>
                        我的模板
                      </el-dropdown-item>
                      <el-dropdown-item command="favorites">
                        <el-icon>
                          <Star />
                        </el-icon>
                        我的收藏
                      </el-dropdown-item>
                      <el-dropdown-item v-if="isAdmin" command="admin-avatar-audit" divided>
                        <el-icon>
                          <User />
                        </el-icon>
                        头像审核管理
                      </el-dropdown-item>
                      <el-dropdown-item command="logout" divided>
                        <el-icon>
                          <SwitchButton />
                        </el-icon>
                        退出登录
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
              <div v-else class="auth-buttons">
                <el-button type="primary" size="small" @click="showLogin = true">
                  登录
                </el-button>
                <el-button size="small" @click="showRegister = true">
                  注册
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main class="app-main">
        <router-view />
      </el-main>

      <!-- 登录表单 -->
      <LoginForm v-model="showLogin" @success="handleLoginSuccess" @switch-to-register="switchToRegister"
        @forgot-password="handleForgotPassword" />

      <!-- 注册表单 -->
      <RegisterForm v-model="showRegister" @success="handleRegisterSuccess" @switch-to-login="switchToLogin"
        @show-terms="handleShowTerms" @show-privacy="handleShowPrivacy" />

      <!-- 忘记密码表单 -->
      <ForgotPasswordForm v-model="showForgotPassword" @success="handleForgotPasswordSuccess" @login="switchToLogin" />

      <!-- 底部 -->
      <el-footer class="app-footer">
        <div class="footer-content">
          <p>&copy; 2024 DevEnv Team. 智能开发环境部署工具 v1.0.0</p>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { ArrowDown, Box, Document, DocumentCopy, House, Shop, Star, SwitchButton, User } from '@element-plus/icons-vue'
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import ForgotPasswordForm from './components/auth/ForgotPasswordForm.vue'
import LoginForm from './components/auth/LoginForm.vue'
import RegisterForm from './components/auth/RegisterForm.vue'
import NotificationBell from './components/NotificationBell.vue'
import { useUserStore } from './stores/user'

const router = useRouter()
const userStore = useUserStore()

// 对话框状态
const showLogin = ref(false)
const showRegister = ref(false)
const showForgotPassword = ref(false)

// 计算属性：检查是否为管理员
const isAdmin = computed(() =>
{
  return userStore.user &&
    (userStore.user.role === 'ADMIN' || userStore.user.role === 'SUPER_ADMIN')
})

// 用户操作处理
const handleUserCommand = (command) =>
{
  switch (command)
  {
    case 'profile':
      router.push('/profile')
      break
    case 'notifications':
      router.push('/notifications')
      break
    case 'templates':
      router.push('/templates')
      break
    case 'favorites':
      router.push('/my-favorites')
      break
    case 'admin-avatar-audit':
      router.push('/admin/avatar-audit')
      break
    case 'logout':
      userStore.logout()
      break
  }
}

// 登录成功处理
const handleLoginSuccess = () =>
{
  showLogin.value = false
}

// 注册成功处理
const handleRegisterSuccess = () =>
{
  showRegister.value = false
}

// 切换到注册
const switchToRegister = () =>
{
  showLogin.value = false
  showRegister.value = true
}

// 切换到登录
const switchToLogin = () =>
{
  showRegister.value = false
  showForgotPassword.value = false
  showLogin.value = true
}

// 忘记密码处理
const handleForgotPassword = () =>
{
  showLogin.value = false
  showForgotPassword.value = true
}

// 忘记密码成功处理
const handleForgotPasswordSuccess = () =>
{
  console.log('忘记密码流程完成')
}

// 显示用户协议
const handleShowTerms = () =>
{
  // TODO: 显示用户协议
  console.log('显示用户协议')
}

// 显示隐私政策
const handleShowPrivacy = () =>
{
  // TODO: 显示隐私政策
  console.log('显示隐私政策')
}

// 初始化时获取用户信息
onMounted(async () =>
{
  if (userStore.token)
  {
    await userStore.getCurrentUser()
  }
})
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
  height: 100vh;
}

.app-container {
  height: 100vh;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0;
  height: 70px !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
  max-width: 1400px;
  margin: 0 auto;
  min-width: 0;
  gap: 20px;
  /* 增加间距 */
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  min-width: 0;
  flex: 0 0 auto;
  /* 固定logo区域大小 */
}

.app-logo {
  height: 40px;
  width: auto;
  object-fit: contain;
}

.app-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: white;
}

.nav-section {
  display: flex;
  align-items: center;
  gap: 20px;
  flex: 1;
  min-width: 0;
}

.nav-menu {
  background: transparent !important;
  border: none !important;
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: flex-end;
}

.user-section {
  flex-shrink: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 10px;
  color: white;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 20px;
  transition: all 0.3s ease;
  border: 2px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.1);
}

.user-dropdown:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.4);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.username {
  font-size: 14px;
  font-weight: 500;
}

/* 下拉菜单样式优化 */
:deep(.el-dropdown-menu) {
  border-radius: 12px;
  border: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  padding: 8px 0;
  margin-top: 8px;
}

:deep(.el-dropdown-menu__item) {
  padding: 12px 20px;
  font-size: 14px;
  color: #2c3e50;
  transition: all 0.3s ease;
  border-radius: 8px;
  margin: 2px 8px;
}

:deep(.el-dropdown-menu__item:hover) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

:deep(.el-dropdown-menu__item.is-divided) {
  border-top: 1px solid #e4e7ed;
  margin-top: 8px;
  padding-top: 12px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 8px;
  font-size: 16px;
}

.auth-buttons {
  display: flex;
  gap: 12px;
}

.auth-buttons .el-button {
  border-radius: 20px;
  padding: 8px 20px;
  font-weight: 500;
  transition: all 0.3s ease;
  border: 2px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.auth-buttons .el-button:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.auth-buttons .el-button--primary {
  background: rgba(255, 255, 255, 0.9);
  color: #667eea;
  border-color: transparent;
}

.auth-buttons .el-button--primary:hover {
  background: white;
  color: #764ba2;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 255, 255, 0.3);
}

.nav-menu .el-menu {
  display: -webkit-box !important;
  display: -ms-flexbox !important;
  display: flex !important;
  -webkit-flex-wrap: nowrap !important;
  -ms-flex-wrap: nowrap !important;
  flex-wrap: nowrap !important;
}

/* 防止Element Plus自动折叠菜单 */
.nav-menu .el-menu--horizontal {
  border-bottom: none !important;
}

.nav-menu .el-menu--horizontal .el-menu-item {
  height: 70px;
  line-height: 70px;
}

/* 隐藏可能的折叠按钮 */
.nav-menu .el-menu--horizontal .el-sub-menu__icon-arrow,
.nav-menu .el-menu--horizontal .el-menu-item .el-icon.el-sub-menu__icon-arrow {
  display: none !important;
}

/* 移动端菜单项优化 */
@media (max-width: 768px) {
  .nav-menu .el-menu--horizontal .el-menu-item {
    height: 45px !important;
    line-height: 45px !important;
    border-radius: 6px;
    margin: 0 2px;
    transition: all 0.3s ease;
  }

  .nav-menu .el-menu--horizontal .el-menu-item:hover,
  .nav-menu .el-menu--horizontal .el-menu-item.is-active {
    background: rgba(255, 255, 255, 0.15) !important;
    border-bottom-color: transparent !important;
  }
}

.nav-menu .el-menu-item {
  -webkit-flex-shrink: 0;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  white-space: nowrap;
  color: rgba(255, 255, 255, 0.8) !important;
  border-bottom: 2px solid transparent !important;
  -webkit-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.nav-menu .el-menu-item:hover,
.nav-menu .el-menu-item.is-active {
  color: white !important;
  background: rgba(255, 255, 255, 0.1) !important;
  border-bottom-color: white !important;
}

.app-main {
  background: #f5f7fa;
  padding: 20px;
  min-height: calc(100vh - 130px);
}

/* 移动端主内容区域优化 */
@media (max-width: 768px) {
  .app-main {
    padding: 15px 10px;
    min-height: calc(100vh - 140px);
  }
}

@media (max-width: 375px) {
  .app-main {
    padding: 10px 8px;
    min-height: calc(100vh - 150px);
  }
}

.app-footer {
  background: #2c3e50;
  color: #bdc3c7;
  text-align: center;
  padding: 15px 0;
  height: 60px !important;
}

.footer-content p {
  margin: 2px 0;
  font-size: 12px;
}

.beian-link {
  color: #bdc3c7;
  text-decoration: none;
  transition: color 0.3s ease;
}

.beian-link:hover {
  color: #ffffff;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .app-title {
    font-size: 18px;
  }

  .app-logo {
    height: 36px;
  }
}

@media (max-width: 992px) {
  .header-content {
    max-width: none;
    padding: 0 15px;
    gap: 15px;
  }

  .app-title {
    font-size: 16px;
  }

  .app-logo {
    height: 32px;
  }

  .logo-section {
    gap: 10px;
  }
}

/* 平板端优化 */
@media (max-width: 768px) {
  .app-header {
    height: auto !important;
    min-height: 60px !important;
  }

  .header-content {
    flex-direction: column;
    padding: 12px 15px;
    height: auto;
    min-height: 60px;
    gap: 12px;
  }

  .logo-section {
    width: 100%;
    justify-content: center;
    gap: 8px;
  }

  .app-title {
    font-size: 16px;
  }

  .app-logo {
    height: 28px;
  }

  .nav-section {
    width: 100%;
    justify-content: center;
    gap: 10px;
  }

  .nav-menu {
    width: auto;
    justify-content: center;
  }

  .nav-menu .el-menu {
    justify-content: center;
    flex-wrap: wrap;
    gap: 5px;
  }

  .nav-menu .el-menu-item {
    height: 45px;
    line-height: 45px;
    padding: 0 12px;
    font-size: 14px;
  }

  .user-section {
    margin-top: 5px;
  }
}

/* 大屏手机端优化 (414px - iPhone 6 Plus/7 Plus/8 Plus) */
@media (max-width: 414px) {
  .header-content {
    padding: 10px 12px;
    gap: 10px;
  }

  .app-title {
    font-size: 15px;
  }

  .app-logo {
    height: 26px;
  }

  .logo-section {
    gap: 6px;
  }

  .nav-menu .el-menu-item {
    height: 40px;
    line-height: 40px;
    padding: 0 10px;
    font-size: 13px;
  }

  .nav-menu .el-menu-item span {
    font-size: 13px;
  }

  .user-dropdown {
    padding: 6px 12px;
    font-size: 13px;
  }

  .auth-buttons .el-button {
    padding: 6px 16px;
    font-size: 13px;
  }
}

/* 标准手机端优化 (375px - iPhone 6/7/8/X) */
@media (max-width: 375px) {
  .header-content {
    padding: 8px 10px;
    gap: 8px;
  }

  .app-title {
    font-size: 14px;
  }

  .app-logo {
    height: 24px;
  }

  .logo-section {
    gap: 5px;
  }

  .nav-menu .el-menu-item {
    height: 38px;
    line-height: 38px;
    padding: 0 8px;
    font-size: 12px;
  }

  .nav-menu .el-menu-item span {
    display: none;
    /* 隐藏文字，只显示图标 */
  }

  .user-dropdown {
    padding: 5px 10px;
    font-size: 12px;
  }

  .username {
    display: none;
    /* 隐藏用户名，只显示头像 */
  }

  .auth-buttons .el-button {
    padding: 5px 12px;
    font-size: 12px;
  }
}

/* 小屏手机端优化 (320px - iPhone 5/SE) */
@media (max-width: 320px) {
  .header-content {
    padding: 6px 8px;
    gap: 6px;
  }

  .app-title {
    font-size: 13px;
    max-width: 200px;
  }

  .app-logo {
    height: 22px;
  }

  .nav-menu .el-menu-item {
    height: 36px;
    line-height: 36px;
    padding: 0 6px;
    font-size: 11px;
  }

  .user-dropdown {
    padding: 4px 8px;
    font-size: 11px;
  }

  .auth-buttons {
    gap: 8px;
  }

  .auth-buttons .el-button {
    padding: 4px 10px;
    font-size: 11px;
  }
}
</style>
